<?php

namespace App\Console\Commands\Cruise;

use App\Models\Common\ConfigModel;
use App\Models\Common\FsCompanyModel;
use App\Models\Common\FsCompanyRoleMappingModel;
use App\Models\Common\FsCompanyUserMappingModel;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Console\Command;

class TransferTaskToLatestCompany extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:transfer-task-to-latest-company';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '稽核-转移任务到最新的公司';


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $lists = [
            199 => 203,
            200 => 202,
            198 => 201,
            180 => 197,
            193 => 197,
        ];

        foreach ($lists as $oldCompanyId => $newCompanyId) {

            $this->info($oldCompanyId . ' -----> ' . $newCompanyId);

            $oldCompany = FsCompanyModel::query()->where('id', $oldCompanyId)->first();
            $newCompany = FsCompanyModel::query()->where('id', $newCompanyId)->first();

            // CruiseCheckDetailModel
            $detail = CruiseCheckDetailModel::query()
                                            ->where('companyId', $oldCompany->departmentId)
                                            ->update([
                                                'companyId'   => $newCompany->departmentId,
                                                'companyName' => $newCompany->name
                                            ]);

            $this->info("detail update -> {$detail} rows...");

            // max
            $maxMappingId = FsCompanyUserMappingModel::query()->max('id');

            // FsCompanyUserMappingModel
            FsCompanyUserMappingModel
                ::query()
                ->where('fsCompanyId', $oldCompanyId)
                ->get()
                ->each(function ($item) use ($newCompanyId, $maxMappingId) {

                    $mapping = FsCompanyUserMappingModel::query()->updateOrCreate([
                        'fsCompanyId' => $newCompanyId,
                        'rmsUserId'   => $item['rmsUserId']
                    ]);

                    if ($mapping->id > $maxMappingId) {
                        $this->info("mapping update -> {$mapping->id}");
                    }
                });

            // FsCompanyRoleMappingModel
            FsCompanyRoleMappingModel
                ::query()
                ->where('fsCompanyId', $oldCompanyId)
                ->get()
                ->each(function ($item) use ($newCompanyId, $maxMappingId) {

                    $mapping = FsCompanyRoleMappingModel::query()
                                                        ->where('fsCompanyUserMappingId', $item->fsCompanyUserMappingId)
                                                        ->where('roleId', $item->roleId)
                                                        ->first();
                    if ($mapping) {
                        return;
                    }

                    FsCompanyRoleMappingModel::query()->create([
                        'fsCompanyUserMappingId' => $item->fsCompanyUserMappingId,
                        'fsCompanyId'            => $newCompanyId,
                        'rmsUserId'              => $item->rmsUserId,
                        'roleId'                 => $item->roleId,
                    ]);

                    $this->info("role mapping update -> {$item->fsCompanyUserMappingId}");
                });
        }
    }
}
